package com.lkm.market2_0.m_imple.app_main;

import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskManager;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.app_main.AppMainModule;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.util.ArrayList;
import java.util.List;




 class AppMainModuleImple implements AppMainModule {


    private SteelyardServerModule mSteelyardServerModule = null;

    private SteelyardDataModule mSteelyardDataModule=null;
    private SteelyardStatusManagerModule mSteelyardStatusModule=null;

    public AppMainModuleImple(SteelyardServerModule mSteelyardServerModule, SteelyardStatusManagerModule mSteelyardStatusModule,SteelyardDataModule mSteelyardDataModule) {
        this.mSteelyardServerModule = mSteelyardServerModule;
        this.mSteelyardStatusModule = mSteelyardStatusModule;
        this.mSteelyardDataModule=mSteelyardDataModule;

    }

    @Override
    public Task<String, String, ResponEntity<List<BreedTo>>> syncGoodsButtonToSteelyardForS(final String steelyardMac) {

        LoadBreedForSTask loadBreedForSTask = new LoadBreedForSTask(new DefaultTaskExecutor<String, String, ResponEntity<List<BreedTo>>>(true), ExpandConfig.instance.getTaskManager2()) {
            @Override
            public void onFinish(ResponEntity<List<BreedTo>> re) {
                super.onFinish(re);
                if (re.isSuccess() && re.getData() != null) {
                    if(P.isDebug()){
                        if(UIP.isPrint()){

                            UIP.p("按钮信息："+new Gson().toJson(re.getData()),steelyardMac);
                        }
                    }
                    SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(steelyardMac);
                    if(steelyardInfo!=null){
                        steelyardInfo.breedTo = re.getData();
                    }

                    syncGoodsButtonToSteelyard(steelyardMac,re.getData());
                }
            }
        };
        loadBreedForSTask.setId("loadBreedForSTask" + steelyardMac);
        loadBreedForSTask.action();
        loadBreedForSTask.setParam(steelyardMac);
        loadBreedForSTask.execTask();

        return loadBreedForSTask;
    }






    @Override
    public void syncGoodsButtonToSteelyard(String steelyardMac, List<BreedTo> breedTos) {

        List<GoodsButtonInfoTo> goodsButtonInfoTos = new ArrayList<>(breedTos.size());

        for (BreedTo b : breedTos) {
            GoodsButtonInfoTo goodsButtonInfoTo = new GoodsButtonInfoTo(b.goods_name, b.price);
            goodsButtonInfoTos.add(goodsButtonInfoTo);
        }
        mSteelyardServerModule.setGoodsButton(steelyardMac, goodsButtonInfoTos);
    }


    

    @Override
    public void requestSetSteelyardOkForS(final String steelyardMac,String okPassword) {
        TaskManager taskManager= ExpandConfig.instance.getTaskManager2();
        int[] tasks=taskManager.getRunTask("checkOkPasswordForSTask"+steelyardMac);
        if(tasks!=null){
            for (int taskS:tasks){
                Task task= taskManager.getRunTask(taskS);
                if(task!=null)task.cancel();
            }
        }

        if(P.isDebug()) {
            P.p("====请求验证把台秤设置成ok "+okPassword+" " + steelyardMac);
            if(UIP.isPrint()){
                UIP.p("请求验证把台秤设置成ok " +okPassword, steelyardMac);
            }
        }

        com.lkm.market2_0.m_imple.app_main.CheckOkPasswordForSTask checkOkPasswordForSTask = new com.lkm.market2_0.m_imple.app_main.CheckOkPasswordForSTask(new DefaultTaskExecutor<String, String, ResponEntity<Boolean>>(true), ExpandConfig.instance.getTaskManager2(),steelyardMac) {
            @Override
            public void onFinish(ResponEntity<Boolean> re) {
                super.onFinish(re);
                if(P.isDebug()) {
                    P.p("====请求验证把台秤设置成ok 成功? "+re.getData() + steelyardMac);
                    if(UIP.isPrint()){
                        UIP.p("请求验证把台秤设置成ok 成功? "+re.getData(), steelyardMac);
                    }
                }
                if (re.getData()) {
                    mSteelyardServerModule.setSteelyardOK(steelyardMac);
                }
            }
        };
        checkOkPasswordForSTask.setId("checkOkPasswordForSTask"+steelyardMac);
        checkOkPasswordForSTask.action();
        checkOkPasswordForSTask.setParam(okPassword);
        checkOkPasswordForSTask.execTask();
    }

    @Override
    public void syncAllOrgInfoForS() {
        if (!LoadAppInitForSTask.isLoading()) {
            LoadAppInitForSTask loadAppInitForSTask = LoadAppInitForSTask.load(AppDataCache.getInstance().getPushKey(),false);
        }

    }
}
